//
//  XXSortView.swift
//  OfferManager
//
//  Created by 谢雄 on 2018/1/5.
//  Copyright © 2018年 OFFER. All rights reserved.
//

import UIKit
import SnapKit

protocol XXSortViewDelegate :class {
    func handleSortViewDismiss()
    func handleSortItemClick()
    
}
class XXSortView: UIView, UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate{

    weak var delegate : XXSortViewDelegate?
    
    lazy var tableView:UITableView = {
        let table = UITableView.init(frame:self.frame, style: .plain)
        table.tableFooterView = UIView.init()
        return table
    }()
    
    lazy var grayView :UIView = {
        let view = UIView.init(frame: CGRect(x:0,y:0,width:XXScreenWidth,height:XXScreenHeight))
        view.backgroundColor = UIColor.black.withAlphaComponent(0)
        return view
    }()
    
    lazy var bottomView :UIView = {
        let view :UIView = UIView.init(frame: CGRect.zero)
        return view
    }()
    
    let data :NSArray
    
    init(frame: CGRect ,data:NSArray) {
        self.data = data
        super.init(frame: frame)
        self.loadUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:-
    //MARK:UI
    func loadUI() {
        
//        self.addSubview(self.grayView)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
//        self.grayView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.handleDismiss(_:)))
        tap.numberOfTapsRequired = 1
        tap.delegate = self
        self.addGestureRecognizer(tap)

        self.addSubview(self.tableView)
        self.bringSubview(toFront: self.tableView)
        self.tableView.frame = CGRect(x:0,y:0,width:Int(XXScreenWidth),height:self.data.count * 44)
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }
    
    func layout() {
        
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        if NSStringFromClass((touch.view?.classForCoder)!) == "UITableViewCellContentView" {
            return false
        }
        return true
    }
    
    //MARK:-
    //MARK:customeAction
    @objc func handleDismiss(_ sender: UITapGestureRecognizer) {
//        self.isHidden = true
        self.dismiss()
        self.delegate?.handleSortViewDismiss()
    }
    
    func dismiss(){
      self.removeFromSuperview()
    }
    
    //MARK:-
    //MARK:tableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return XXFilterCell.caculateCellHeight()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:XXFilterCell = XXFilterCell .sortCellWithTable(tableView) as! XXFilterCell
        cell.updateCellWithData(self.data[indexPath.row] as! String)
        return cell
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
